/*
 * #%L
 * Admin
 * %%
 * Copyright (C) 2011 Ractoc.com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.ractoc.opengamefinder.admin;

import com.jme3.app.SimpleApplication;
import com.ractoc.opengamefinder.api.results.MessageResultsErrorCodes;
import com.ractoc.opengamefinder.client.ConfigLocator;
import com.ractoc.opengamefinder.client.GuiContainer;
import com.ractoc.pffj.api.BasePluginMessageResult;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author ractoc
 */
public class OGFAdminStartup extends SimpleApplication {
    private static Logger logger = Logger.getLogger(OGFAdminStartup.class.getName());
    private static OGFAdminStartup application;

    /**
     * Default constructor.
     */
    private OGFAdminStartup() {
    }

    /**
     * Main startup method for the client.
     * 
     * @param args
     *            arguments supplied to the client. No arguments are expected or
     *            parsed.
     */
    //CHECKSTYLE:OFF
    public static void main(final String[] args) {
        //CHECKSTYLE:ON
        String log4jConfigFile = ConfigLocator.getCustomConfig("log4j.xml");
        if (log4jConfigFile != null) {
            PropertyConfigurator.configureAndWatch(log4jConfigFile);
        } else {
            PropertyConfigurator.configureAndWatch("log4j.xml");
        }
        application = new OGFAdminStartup();
        application.start();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void simpleInitApp() {

        // first we setup the GUI container. This way, the guiPlugin has all
        // that it needs to build the GUI.
        final GuiContainer guiContainer = GuiContainer.getInstance();
        guiContainer.setAssetManager(assetManager);
        guiContainer.setAudioRenderer(audioRenderer);
        guiContainer.setGuiViewPort(guiViewPort);
        guiContainer.setInputManager(inputManager);

        try {
            BasePluginMessageResult dsmResult = AdminFactory.createInstance(guiContainer);
            if (dsmResult.getErrorCode() != MessageResultsErrorCodes.SUCCESS) {
                logger.error("error code: " + dsmResult.getErrorCode());
                System.exit(dsmResult.getErrorCode().ordinal());
            }
        }
        catch (OGFAdminException e) {
            logger.error("Error in OGF", e);
            System.exit(-1);
        }

        // disable the fly cam
        flyCam.setDragToRotate(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void destroy() {
        super.destroy();
        System.exit(0);
    }
}
